package com.demo;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSession;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

public class HttpsUtils {

    public static String send(String signData, String signURL) {

        URL myurl = null;
        try {
            myurl = new URL(signURL);
        } catch (MalformedURLException e1) {
            System.out.println("url error:" + e1);
        }

        HttpsURLConnection con = null;
        try {
            con = (HttpsURLConnection) myurl.openConnection();
            con.setSSLSocketFactory(new TLSSocketConnectionFactory());
            /*用于解决host name wrong问题，重写主机验证方法，如果请求正常可以去掉*/
            con.setHostnameVerifier(new HostnameVerifier() {
                @Override
                public boolean verify(String hostname, SSLSession session) {
                    return true;
                }
            });

            System.setProperty("sun.net.client.defaultConnectTimeout", "300000");
            System.setProperty("sun.net.client.defaultReadTimeout", "300000");
            con.setRequestMethod("GET");
            con.setDoOutput(true);
            con.setDoInput(true);
            con.setUseCaches(false);
            con.setInstanceFollowRedirects(true);
            con.setRequestProperty("Content-type", "text/html;charset=utf-8");
            con.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
            System.out.println("start connect");
            con.connect();
            System.out.println("end connect");
        } catch (IOException e1) {
            System.err.println("Https URLConnection error:" + e1);
        }

        /*OutputStream out;
        //OutputStreamWriter os = null;
        try {

            out = con.getOutputStream();
            //os = new OutputStreamWriter(out);
            out.write(signData.getBytes("utf-8"));
            out.flush();
            out.close();
            //os.write(signData);

            //os.flush();
        } catch (IOException e1) {
            System.out.println("IOException:" + e1);

        } catch (Exception ex) {
            System.out.println("HTTPS Exception:" + ex);
            System.out.println(ex.getStackTrace());
        }*/

        //接收响应
        StringBuffer sb = new StringBuffer();
        try {
            System.out.println("response code=" + con.getResponseCode());
            BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream(), "utf-8"));
            String s = reader.readLine();
            while (s != null) {
                sb.append(s);
                s = reader.readLine();
            }
            reader.close();

            con.disconnect();
        } catch (IOException ioex) {
            System.err.println("https write file error:" + ioex);
        }

        return sb.toString();
    }
}